package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Robot;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 验证机器人是否可用回调
 * <p>
 * scene_id：场最ID，默认为3。
 * 1：主加+私聊＋入群+群聊；
 * 2：被加+私聊+入群+群聊；
 * 3：被加且被邀请进群；
 * 4：主加目主动邀请进群；
 * 5：主加+主拉+被加＋被拉
 *
 * <pre>
 * {
 *     "Data": {
 *         "scene_id": 1,
 *         "robot_serial_nos": [
 *             "14BC26C1F23A77A07760672146180AF5",
 *             "DA1B048A0F72A1142FE1F29C982E3B79",
 *             "49078B765360BAA6231AB8C72EB91E00",
 *             "B9242BBFE29699C36B54435F5F407F2F",
 *             "0C0265BA3998B435679B3270F2DE5AE5",
 *             "2E0E70DFEE87E294DB8988964E223FFD"
 *         ],
 *         "limit_robot_serial_nos": [
 *             "C512A57EFDB0CB9905E857B44B340A00",
 *             "C6FE5DA5A713ECA14149E433DA3323EC",
 *             "16B715E760AE21D0BD95BDF1EDF0A4A7",
 *             "CA3BDB67ACF1C6B9301F77E040BBA350",
 *             "C8A63C52C67BA0AA2D1B2B9C2EA9C1ED",
 *             "20D4B2ADA0713AB28855D9709513469B",
 *             "2B09AA8BEAE13F9AC80BCF050CBE4AE7",
 *             "711C1B8C93D5D3D755D0AE896CC28FED"
 *         ]
 *     },
 *     "nType": 1203,
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "nTimeStamp": 1738198802067,
 *     "vcSerialNo": "20250130090000825711310053156",
 *     "vcRobotWxId": "",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": ""
 * }
 * </pre>
 */
@Slf4j
@Component
public class _1203Handler implements CallbackHandler {

    @Resource
    private RobotMapper robotMapper;

    @Override
    public String name() {
        return "验证机器人是否可用回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 1203;
    }

    @Override
    public void handle(Callback callback) {
        JSONObject data = (JSONObject) callback.getData();
        List<String> limitRobotSerialNos = data.getList("limit_robot_serial_nos", String.class);
        if (!ObjectUtils.isEmpty(limitRobotSerialNos)) {
            LambdaUpdateWrapper<Robot> wrapper = new LambdaUpdateWrapper<Robot>()
                    .set(Robot::getLimited, true)
                    .in(Robot::getId, limitRobotSerialNos);
            robotMapper.update(wrapper);
        }
    }
}
